﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using V5_Data.Model;

namespace V5_Data {
    /// <summary>
    /// 购物车帮助类
    /// </summary>
    public class V5ProductShoppinCartHelper {
        private static readonly V5ProductShoppinCartHelper m_Instance = new V5ProductShoppinCartHelper();
        /// <summary>
        /// 
        /// </summary>
        public static V5ProductShoppinCartHelper Instance {
            get { return m_Instance; }
        }
        /// <summary>
        /// 总个数
        /// </summary>
        public int Quantity {
            get {
                int _Quantity = 0;
                List = GetProductFromShoppingCart();
                foreach (var item in List) {
                    _Quantity += item.Quantity.Value;
                }
                return _Quantity;
            }
        }
        /// <summary>
        /// 总价格
        /// </summary>
        public int TotalPrice {
            get {
                int _TotalPrice = 0;
                List = GetProductFromShoppingCart();
                foreach (var item in List) {
                    _TotalPrice += item.TotalPrice.Value;
                }
                return _TotalPrice;
            }
        }
        private List<ModelShoppingCart> List = new List<ModelShoppingCart>();
        public void AddProductToShoppingCart(
            int ProductID,
            string ProductName,
            string ProductPhoto,
            int ProductScore,
            string ProductShortInfo,
            int MarketPrice, int MemberPrice, int Quantity, int TotalPrice
      ) {
            ModelShoppingCart model = new ModelShoppingCart();
            model.ProductID = ProductID;
            model.ProductName = ProductName;
            model.ProductPhoto = ProductPhoto;
            model.ProductScore = ProductScore;
            model.ProductShortInfo = ProductShortInfo;
            model.MarketPrice = MarketPrice;
            model.MemberPrice = MemberPrice;
            model.Quantity = Quantity;
            model.TotalPrice = TotalPrice;
            AddProductToShoppingCart(model);
        }
        /// <summary>
        /// 添加产品到购物车
        /// </summary>
        /// <param name="model"></param>
        public void AddProductToShoppingCart(ModelShoppingCart model) {
            List = GetProductFromShoppingCart();
            if (List.Count == 0) {
                List.Add(model);
            }
            if (List.Where(p => p.ProductID == model.ProductID).Count() == 0) {
                List.Add(model);
            }
            SaveShoppingCart(List);
        }
        /// <summary>
        /// 从购物车中删除一个产品
        /// </summary>
        public void DelProductFromShoppingCart(int ProductID) {
            List = GetProductFromShoppingCart();
            var model = List.Where(p => p.ProductID == ProductID).FirstOrDefault();
            List.Remove(model);
            SaveShoppingCart(List);
        }
        /// <summary>
        /// 修改某产品的数量
        /// </summary>
        /// <param name="ProductID"></param>
        /// <param name="Num"></param>
        public void EditProductFromShoppingCart(int ProductID, int Num) {
            List = GetProductFromShoppingCart();
            var model = List.Where(p => p.ProductID == ProductID).FirstOrDefault();
            if (model.Quantity.Value + Num > 0) {
                model.Quantity = model.Quantity.Value + Num;
                model.TotalPrice = model.Quantity.Value * model.MemberPrice;
            }
            SaveShoppingCart(List);
        }
        /// <summary>
        /// 重购物车获取所有产品信息
        /// </summary>
        /// <returns></returns>
        public List<ModelShoppingCart> GetProductFromShoppingCart() {
            if (HttpContext.Current.Session["ShoppingCart"] != null) {
                List = (List<ModelShoppingCart>)HttpContext.Current.Session["ShoppingCart"];
                return List;
            }
            return new List<ModelShoppingCart>();
        }

        /// <summary>
        /// 保存购物车信息
        /// </summary>
        /// <param name="List"></param>
        private void SaveShoppingCart(List<ModelShoppingCart> List) {
            HttpContext.Current.Session["ShoppingCart"] = List;
        }
        /// <summary>
        /// 清空购物车
        /// </summary>
        public void RemoveShoppingCart() {
            HttpContext.Current.Session.Remove("ShoppingCart");
        }

        #region 用户地址
        //private BLLUserToArea bllUserToArea = new BLLUserToArea();
        ///// <summary>
        ///// 获取用户购物车
        ///// </summary>
        //public DataTable Query_By_ShoppingAddress(string what, string where, string orderby, int limit) {
        //    DataTable dt = bllUserToArea.GetList(what, limit, where, orderby).Tables[0];
        //    return dt;
        //}
        ///// <summary>
        ///// 获取地址详细
        ///// </summary>
        //public ModelUserToArea GetModel_Address(int ID) {
        //    return bllUserToArea.GetModel(ID);
        //}
        /// <summary>
        /// 使用新地址
        /// </summary>
        public void Use_Address() {

        }

        #endregion
    }
}